//
//  LGAudioMultipleView.m
//  EducationCollection
//
//  Created by mac on 2021/6/10.
//  倍数播放

#import "LGAudioMultipleView.h"
@interface LGAudioMultipleView()
/**   */
@property (nonatomic , strong)UIView *baseView;
/**   */
@property (nonatomic , strong)UILabel *titleLabel;
/**   */
@property (nonatomic , strong)UIView *lineView;
/**   */
@property (nonatomic , strong)UIView *bottomLineView;
/**   */
@property (nonatomic , strong)UIButton *closeBtn;
/**   */
@property (nonatomic , strong)NSArray *dataArray;
/**   */
@property (nonatomic , strong)NSMutableArray *buttonArray;
/**   */
@property (nonatomic , assign)NSInteger index;
@end
@implementation LGAudioMultipleView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.dataArray = @[
        @{@"title":@"0.75倍数",@"multiple":@"0.75"},
        @{@"title":@"正常倍数",@"multiple":@"1.0"},
        @{@"title":@"1.25倍数",@"multiple":@"1.25"},
        @{@"title":@"1.5倍数",@"multiple":@"1.5"},
        @{@"title":@"2.0倍数",@"multiple":@"2.0"},
        ];
        [self createSubView];
    }
    return self;
}

-(void)setIndex:(NSInteger)index{
    _index = index;
    for (NSInteger i=0; i<self.buttonArray.count; i++) {
        UIButton *button = self.buttonArray[i];
        button.selected = (index==i)?YES:NO;
        [button setTitle:self.dataArray[i][@"title"] forState:UIControlStateNormal];
    }
}

-(void)timeButtonAction:(UIButton *)sender{
    self.index = sender.tag-1000;
    NSDictionary *tempDic = self.dataArray[self.index];
    self.multiple = [tempDic[@"multiple"] floatValue];
    if (self.delegate && [self.delegate respondsToSelector:@selector(selectMultiple:)]) {
        [self.delegate selectMultiple:self.multiple];
    }
    [self closeBtnAction];
}

-(void)closeBtnAction{
    [UIView animateWithDuration:0.4 animations:^{
        self.baseView.frame = CGRectMake(0, Screen_H, Screen_W, viewPix(350)+bottomSafeBarHeight/2.0);
        self.backgroundColor = [UIColor colorWithString:@"#000000" alpha:0];
    }completion:^(BOOL finished) {
        self.hidden = YES;
    }];
}

-(void)showMultipleViewAnimation{
    self.hidden = NO;
    [UIView animateWithDuration:0.3 animations:^{
        self.baseView.frame = CGRectMake(0, Screen_H-(viewPix(335)+bottomSafeBarHeight/2.0), Screen_W, viewPix(350)+bottomSafeBarHeight/2.0);
        self.backgroundColor = [UIColor colorWithString:@"#000000" alpha:0.5];
    }];
}

#pragma mark
#pragma mark ====> 创建控件
-(void)createSubView{
    [self addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(closeBtnAction)]];
    self.backgroundColor = [UIColor colorWithString:@"#000000" alpha:0];
    [self addSubview:self.baseView];
    [self.baseView addSubview:self.titleLabel];
    [self.baseView addSubview:self.lineView];
    [self.baseView addSubview:self.bottomLineView];
    [self.baseView addSubview:self.closeBtn];
    [self createTimeButton];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.baseView);
        make.top.equalTo(self.baseView);
        make.height.equalTo(@(viewPix(45)));
    }];
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.baseView).offset(viewPix(15));
        make.right.equalTo(self.baseView).offset(-viewPix(15));
        make.top.equalTo(self.titleLabel.mas_bottom);
        make.height.equalTo(@(0.5));
    }];
    [self.bottomLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.height.equalTo(self.lineView);
        make.bottom.equalTo(self.closeBtn.mas_top);
    }];
    [self.closeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.baseView);
        make.bottom.equalTo(self.baseView).offset(-(bottomSafeBarHeight/2.0+viewPix(15)));
        make.height.equalTo(@(viewPix(50)));
        make.width.equalTo(@(Screen_W));
    }];
}

-(void)createTimeButton{
    CGFloat height = (viewPix(335)-viewPix(45)-viewPix(50)-viewPix(15))/self.dataArray.count;
    for (NSInteger i=0; i<self.dataArray.count; i++) {
        NSDictionary *tempDic = self.dataArray[i];
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(0, viewPix(45)+viewPix(7)+height*i, Screen_W, height);
        [button setTitle:tempDic[@"title"] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor colorWithString:@"#333333"] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor colorWithString:@"#1DBD9C"] forState:UIControlStateSelected];
        button.titleLabel.font = LGFont(14);
        button.tag = 1000+i;
        [button addTarget:self action:@selector(timeButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.baseView addSubview:button];
        [self.buttonArray addObject:button];
    }
}

-(NSMutableArray *)buttonArray{
    if (!_buttonArray) {
        _buttonArray = [NSMutableArray array];
    }
    return _buttonArray;
}

-(UIView *)baseView{
    if(!_baseView){
        _baseView = [[UIView alloc]initWithFrame:CGRectMake(0, Screen_H, Screen_W, viewPix(350)+bottomSafeBarHeight/2.0)];
        _baseView.backgroundColor = [UIColor whiteColor];
        _baseView.cornerRidus = viewPix(15);
    }
    return _baseView;
}

-(UILabel *)titleLabel{
    if(!_titleLabel){
        _titleLabel = [UILabel lableWithText:@"倍数播放" colorString:@"#333333" textFont:LGFontWeight(17, UIFontWeightSemibold) textAlignment:NSTextAlignmentCenter lines:1];
    }
    return _titleLabel;
}

-(UIView *)lineView{
    if(!_lineView){
        _lineView = [[UIView alloc]init];
        _lineView.backgroundColor = [UIColor colorWithString:@"#DDDDDD"];
    }
    return _lineView;
}

-(UIView *)bottomLineView{
    if(!_bottomLineView){
        _bottomLineView = [[UIView alloc]init];
        _bottomLineView.backgroundColor = [UIColor colorWithString:@"#EEEEEE"];
    }
    return _bottomLineView;
}

-(UIButton *)closeBtn{
    if(!_closeBtn){
        _closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_closeBtn setTitleColor:[UIColor colorWithString:@"#333333"] forState:UIControlStateNormal];
        [_closeBtn setTitleColor:[UIColor colorWithString:@"#333333"] forState:UIControlStateHighlighted];
        [_closeBtn setTitle:@"关闭" forState:UIControlStateNormal];
        [_closeBtn setTitle:@"关闭" forState:UIControlStateHighlighted];
        _closeBtn.titleLabel.font = LGFont(16);
        [_closeBtn addTarget:self action:@selector(closeBtnAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _closeBtn;
}


@end
